package com.yxjx.duoxue.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

public class MacAddrHelper {
	/**
	 * 通过wifiManager获取mac地址
	 * @attention Wifi
	 * @return Mac Address
	 */
	public static String getMacFromWifi(Context context){
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);   
        if (!wifiManager.isWifiEnabled()) {   
        	wifiManager.setWifiEnabled(true);    
        }   
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();       
        String mResult = wifiInfo.getMacAddress();
        Utils.logd("Mac address(wifi): "+mResult);
        return mResult;
	}
	

	/**
	 * 通过callCmd("busybox ifconfig","HWaddr")获取mac地址
	 * @attention 需要设备装有busybox工具
	 * @return Mac Address
	 */
	public static String getMacFromCallCmd(){
		String result = "";
        result = callCmd("busybox ifconfig","HWaddr");
        
        if(result == null || result.length() <= 0){
        	Utils.logd("callCmd returns null or empty");
        	return null;
        }
        
        //对该行数据进行解析
        //例如：eth0      Link encap:Ethernet  HWaddr 00:16:E8:3E:DF:67
        if(result.length()>0 && result.contains("HWaddr")==true){
        	String Mac = result.substring(result.indexOf("HWaddr")+6, result.length()-1);
        	Utils.logd("Mac:"+Mac+" Mac.length: "+Mac.length());  
        	if(Mac.length()>1){
        		Mac = Mac.replaceAll(" ", "");
        		result = "";
        		
        		String[] tmp = Mac.split(":");
        		for(int i = 0;i<tmp.length;++i){
        			result +=tmp[i];
        		}
        	}
        	Utils.logd(result+" result.length: "+result.length());
        }
    	Utils.logd("Mac address(CallCmd): "+result);

        return result;
	}
	
	  public static String callCmd(String cmd,String filter) {   
	        String result = "";   
	        String line = "";   
	        try {
	        	Process proc = Runtime.getRuntime().exec(cmd);
	            InputStreamReader is = new InputStreamReader(proc.getInputStream());   
	            BufferedReader br = new BufferedReader (is);   
	            
	            //执行命令cmd，只取结果中含有filter的这一行
	            while ((line = br.readLine ()) != null && line.contains(filter)== false) {   
	            	//result += line;
	            	Utils.logd("line: "+line);
	            }
	            
	            result = line;
	            Utils.logd("result: "+result);
	        }   
	        catch(Exception e) {   
	            e.printStackTrace();   
	        }   
	        return result;   
	    } 
	  
	 
	   public static String getMacAddress() {
	        String result = "";
	        String Mac = "";
	        result = callCmd("busybox ifconfig", "HWaddr");
	 
	        if (result == null) {
	            return "网络出错，请检查网络";
	        }
	        if (result.length() > 0 && result.contains("HWaddr")) {
	            Mac = result.substring(result.indexOf("HWaddr") + 6, result.length() - 1);
	            if (Mac.length() > 1) {
	                result = Mac.toLowerCase();
	            }
	        }
	        return result.trim();
	    }
	
}
